package com.sscaipiao.android.adapter;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;

import android.content.Context;
import android.graphics.Color;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.sscaipiao.android.R;
import com.sscaipiao.android.model.TogetherBuy;
import com.sscaipiao.android.utils.StringUtil;
import com.sscaipiao.android.view.TogetherBuyHallView;


public class TogetherBuyItemAdapt extends BaseAdapter {

	private List<TogetherBuy> mArraylist;
	private Context context;
	public  List<TogetherBuy> mOrderedlist;
	private int mOrderKey;

	public static final int ORDER_UP = 1;
	public static final int ORDER_DOWN = 2;
	public static final int ORDERBY_CanYuJinDu = 1;
	public static final int ORDERBY_FangAnZongE = 2;
	public static final int ORDERBY_MeiFenJinE = 3;
	public static final int ORDERBY_FanJiagLv = 4;

	public TogetherBuyItemAdapt(Context c, List<TogetherBuy> list) {
		this.context = c;
		this.mArraylist = list;
		mOrderedlist = sortHeMaiListByKey(ORDERBY_CanYuJinDu,ORDER_DOWN);

	}
	public void setOrderKey(int key, int order){
		mOrderKey = key;
		mOrderedlist = sortHeMaiListByKey(mOrderKey,order);
		notifyDataSetChanged();
	}
	@Override
	public int getCount() {

		return mArraylist.size();
	}

	@Override
	public Object getItem(int arg0) {

		return mOrderedlist.get(arg0);
	}

	@Override
	public long getItemId(int arg0) {
		return arg0;
	}
	static class ViewHolder {
		public TextView hemai_tg_item_percent;
		public TextView hemai_tg_item_author;
		public TextView hemai_tg_item_total_amount;
		public TextView hemai_tg_item_each_amount;
		public TextView hemai_tg_item_return_amount;
		public TextView hemai_tg_item_total;
		public TextView hemai_tg_item_each;
		public TextView hemai_tg_item_return;
		public TextView hemai_tg_item_baodi;
	}
	@Override
	public View getView(int arg0, View convertView, ViewGroup parent) {
		ViewHolder holder;
		if (convertView == null) {
			convertView = (View) LayoutInflater.from(context).inflate(R.layout.together_hall_item, null);
			holder = new ViewHolder();
			holder.hemai_tg_item_percent = (TextView) convertView.findViewById(R.id.hemai_tg_item_percent);
			holder.hemai_tg_item_author = (TextView) convertView.findViewById(R.id.hemai_tg_item_author);
			holder.hemai_tg_item_total_amount = (TextView) convertView.findViewById(R.id.hemai_tg_item_total_amount);
			holder.hemai_tg_item_each_amount = (TextView) convertView.findViewById(R.id.hemai_tg_item_each_amount);
			holder.hemai_tg_item_return_amount = (TextView) convertView.findViewById(R.id.hemai_tg_item_return_amount);
			holder.hemai_tg_item_total = (TextView) convertView.findViewById(R.id.hemai_tg_item_total);
			holder.hemai_tg_item_each = (TextView) convertView.findViewById(R.id.hemai_tg_item_each);
			holder.hemai_tg_item_return = (TextView) convertView.findViewById(R.id.hemai_tg_item_return);
			holder.hemai_tg_item_baodi = (TextView) convertView.findViewById(R.id.hemai_baodi);
			convertView.setTag(holder);
		}   else {
			holder = (ViewHolder) convertView.getTag();
		}
		if (mOrderedlist.size() == 0) {
			return convertView;
		}
		TogetherBuy item = mOrderedlist.get(arg0);
		if (item != null) {
			//mOrderedlist = getDataByKey(mOrderKey,true);
			holder.hemai_tg_item_percent.setText(item.getPercent());
			holder.hemai_tg_item_author.setText(item.getPromoter());
			holder.hemai_tg_item_total_amount.setText(item.getBetMoney()+"元");
			holder.hemai_tg_item_each_amount.setText(item.getPrice()+"元");
			holder.hemai_tg_item_return_amount.setText(item.getFee());
			if (StringUtil.isNotEmptyString(item.getBaodi())) {
				holder.hemai_tg_item_baodi.setText(item.getBaodi());
			}
			int color = context.getResources().getColor(R.color.TextColorGray);
			holder.hemai_tg_item_total.setTextColor(color);
			holder.hemai_tg_item_each.setTextColor(color);
			holder.hemai_tg_item_return.setTextColor(color);
			switch (mOrderKey) {
				case ORDERBY_FangAnZongE:
					holder.hemai_tg_item_total.setTextColor(Color.RED);
					break;
				case ORDERBY_FanJiagLv:
					holder.hemai_tg_item_return.setTextColor(Color.RED);
					break;
				case ORDERBY_MeiFenJinE:
					holder.hemai_tg_item_each.setTextColor(Color.RED);
					break;
			}
		}


		return convertView;

	}
	private List<TogetherBuy> sortHeMaiListByKey(final int key, final int up) {
		if (mArraylist.isEmpty()) {
			return mArraylist;
		}
		try {
			Collections.sort(mArraylist, new Comparator<TogetherBuy>() {
				@Override
				public int compare(TogetherBuy object1, TogetherBuy object2) {
					if (key == ORDERBY_CanYuJinDu) {
						if (up == ORDER_UP) {
							return (((Integer)Integer.valueOf(object1.getPercent())).compareTo((Integer)Integer.valueOf( object2.getPercent())));
						} else {
							return (((Integer)Integer.valueOf(object2.getPercent())).compareTo((Integer)Integer.valueOf( object1.getPercent())));
						}
					}
					if (key == ORDERBY_FangAnZongE) {
						if (up == ORDER_UP) {
							return (((Integer)Integer.valueOf(object1.getBetMoney())).compareTo((Integer)Integer.valueOf( object2.getBetMoney())));
						} else {
							return (((Integer)Integer.valueOf(object2.getBetMoney())).compareTo((Integer)Integer.valueOf( object1.getBetMoney())));
						}
					}
					if (key == ORDERBY_MeiFenJinE) {
						if (up == ORDER_UP) {
							return (((Integer)Integer.valueOf(object1.getPrice())).compareTo((Integer)Integer.valueOf( object2.getPrice())));
						} else {
							return (((Integer)Integer.valueOf(object2.getPrice())).compareTo((Integer)Integer.valueOf( object1.getPrice())));
						}
					}
					if (key == ORDERBY_FanJiagLv) {
						if (up == ORDER_UP) {
							return (((Integer)Integer.valueOf(object1.getPercent())).compareTo((Integer)Integer.valueOf( object2.getPercent())));
						} else {
							return (((Integer)Integer.valueOf(object2.getPercent())).compareTo((Integer)Integer.valueOf( object1.getPercent())));
						}
					}
					return 0;
				}
			});
		} catch (Exception e) {

		}

		return mArraylist;
	}

}
